<?php
class iKernel implements iController{
	public $navigation_module;
	public $pref;
	function iKernel(){	
			 foreach ($_GET as $check_url) {
                    	if (!is_array($check_url)) {
                    		$check_url = str_replace("\"", "", $check_url);
                    		if ((preg_match("/<[^>]*script*\"?[^>]*>/i", $check_url)) || (preg_match("/<[^>]*object*\"?[^>]*>/i", $check_url)) ||
                    			(preg_match("/<[^>]*iframe*\"?[^>]*>/i", $check_url)) || (preg_match("/<[^>]*applet*\"?[^>]*>/i", $check_url)) ||
                    			(preg_match("/<[^>]*meta*\"?[^>]*>/i", $check_url)) || (preg_match("/<[^>]*style*\"?[^>]*>/i", $check_url)) ||
                    			(preg_match("/<[^>]*form*\"?[^>]*>/i", $check_url)) || (preg_match("/\([^>]*\"?[^)]*\)/i", $check_url)) ||
                    			(preg_match("/\"/i", $check_url))) {
                    		die ();
                    		}
                    	}
                    }
            unset($check_url);
					
						$this->pref = new DB("settings");
						$this->pref->select();
						$this->pref->nextRecord();
						
					
						$db = new DB("admin_cache");
						$db->setColPrefix("admin_");
						$db->delete("admin_time<".(time()-260));

						
						  $referer = $_SERVER['HTTP_REFERER'];
						  $browser = $_SERVER['HTTP_USER_AGENT'];
						
						
							$db = new DB("unique_visitors");
							$db->setColPrefix("visitor_");
							$db->select("visitor_ip='".USER_IP."'");
							if($db->numRows() != "1"){
								$db->ip = USER_IP;
								$db->referrer = $referer;
								$db->browser = $browser;
								$db->when = time();
								$db->insert();
							}
						
						
						$url = new iUrl;
						$page = $url->page_url;
						$argv1 = $url->var_a;
						$this->nav = new DB("navigation");
						$this->nav->select();
						$this->nav->setColPrefix("menu_");
						while($this->nav->nextRecord()){
							$current = "";
							if($page == "custom" && $this->nav->argv1 == $argv1) $current = "id='current'";
														
							if($this->nav->argv1 != ""){
								$this->nav_argv1 = "&argv1=".$this->nav->argv1;
							}else{
								$this->nav->argv1 = "";
							}
							if($this->nav->argv2 != ""){
								$this->nav->argv2 = "&argv2=".$this->nav->argv2;
							}else{
								$this->nav->argv2 = "";
							}
							if($this->nav->argv3 != ""){
								$this->nav->argv3 = "&argv3=".$this->nav->argv3;
							}else{
								$this->nav->argv3 = "";
							}
							
							
								if($page == $this->nav->control && $page != "custom") $current = "id='current'";
							$this->navigation_module .="<li> <a title='".$this->nav->title."' href='".iUrl::makeClean("?lang_id=en&page_url=".$this->nav->control."/".$this->nav->argv1.$this->nav->argv2.$this->nav->argv3)."' $current>".$this->nav->title."</a></li>";
					
						}
		
		
				if($this->login()){
                	$cookie_vars = explode(".", $_COOKIE[COOKIE_PREFIX.'user']);
                	$cookie_1 = $this->isnum($cookie_vars['0']) ? $cookie_vars['0'] : "0";
                	$cookie_2 = ($this->preg_check("/^[0-9a-z]{32}$/", $cookie_vars['1']) ? $cookie_vars['1'] : "");

					
					$this->userdata = new DB("users");
					$this->userdata->setColPrefix("user_");
					$this->userdata->select("user_id='".$cookie_1."' and user_password='".$cookie_2."'");
					$this->userdata->nextRecord();
					
                    define("USER_ID", $this->userdata->id);
                    if($this->userdata->auth == "2"){ 
                    	define('iADMIN', true);
                        define('iCUSTOM', true);
                    	define('iUSER', true);
                    }
                    if($this->userdata->auth == "1"){
                        define('iADMIN', false);
                    	define('iCUSTOM', true); 
                    	define('iUSER', true);
                    }
                    if($this->userdata->auth == "0"){ 
                    	define('iADMIN', false);
                        define('iCUSTOM', false); 
                    	define('iUSER', true);
                    }
                
                
                    if (!isset($_COOKIE[COOKIE_PREFIX.'lastvisit']) || !$this->isnum($_COOKIE[COOKIE_PREFIX.'lastvisit'])) {
                				setcookie(COOKIE_PREFIX."lastvisit", $this->userdata->lastvisit, time() + 3600, "/", "", "0");			
                				$lastvisited = $this->userdata->lastvisit;
                	}else{
                				$lastvisited = $_COOKIE[COOKIE_PREFIX.'lastvisit'];
                	}
                }else{
                    define('iADMIN', false);
                    define('iCUSTOM', false);
                    define('iUSER', false);
                    define('iGUEST', true);
                }
				
				 if (iUSER) {
				 		$this->online = new DB("online");
						$this->online->select("online_user='".$this->userdata->id."'");
						$this->online->setColPrefix("online_");	
								  
                        if ($this->online->numRows() == 0) {
						   	$db = new DB("online");
							$db->setColPrefix("online_");
							$db->user = $this->userdata->id;
							$db->ip = USER_IP;
							$db->lastactive = time();
							$db->insert(); 
                        }else{
							$db = new DB("online");
							$db->setColPrefix("online_");
							$db->lastactive = time();
							$db->update("online_user=".$this->userdata->id);
                        }
                
                }

							$db = new DB("online");
							$db->setColPrefix("online_");
							$db->delete("online_lastactive<".(time()-60));
		
	}

	function login(){
    	if (!isset($_COOKIE[COOKIE_PREFIX.'user'])) return false; else return true;
	}
	
	function isnum($value) {
		if (!is_array($value)) {
			return (preg_match("/^[0-9]+$/", $value));
		} else {
			return false;
		}
	}
	
	function preg_check($expression, $value) {
		if (!is_array($value)) {
			return preg_match($expression, $value);
		} else {
			return false;
		}
	}

	function makefilelist($folder, $filter, $sort=true, $type="files") {
		$res = array();
		$filter = explode("|", $filter); 
		$temp = opendir($folder);
		while ($file = readdir($temp)) {
			if ($type == "files" && !in_array($file, $filter)) {
				if (!is_dir($folder.$file)) { $res[] = $file; }
			} elseif ($type == "folders" && !in_array($file, $filter)) {
				if (is_dir($folder.$file)) { $res[] = $file; }
			}
		}
		closedir($temp);
		if ($sort) { sort($res); }
		return $res;
	}
	
	function makefileopts($files, $selected = "") {
		$res = "";
		for ($i = 0; $i < count($files); $i++) {
			$sel = ($selected == $files[$i] ? " selected='selected'" : "");
			$res .= "<option value='".$files[$i]."'$sel>".$files[$i]."</option>\n";
		}
		return $res;
	}
	
	function findexts($filename) { 
		 $filename = strtolower($filename) ; 
		 $exts = split("[/\\.]", $filename) ; 
		 $n = count($exts)-1; 
		 $exts = $exts[$n]; 
		 return $exts; 
	 } 
	
	function toAscii($str, $replace=array(), $delimiter='-') {
		if( !empty($replace) ) {
			$str = str_replace((array)$replace, ' ', $str);
		}
	
		$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
		$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
		$clean = strtolower(trim($clean, '-'));
		$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
	
		return $clean;
	}
	
	function redirect($location){
				echo "<script type='text/javascript'>document.location.href='".str_replace("&amp;", "&", $location)."'</script>\n";
				exit;
		}

	function getUser($row, $id){
		$db = new DB("users");
		$db->setColPrefix("user_");
		$db->select("user_id='$id'");
		$db->nextRecord();
		return $db->$row;
	}
	 

}


	function parsesmileys($message) {
		$smiley = array(
			"#\:\)#si" => "<img src='".IMAGES."smiley/smile.gif' alt='smiley' />",
			"#\;\)#si" => "<img src='".IMAGES."smiley/wink.gif' alt='smiley' />",
			"#\:\(#si" => "<img src='".IMAGES."smiley/sad.gif' alt='smiley' />",
			"#\:\|#si" => "<img src='".IMAGES."smiley/frown.gif' alt='smiley' />",
			"#\:o#si" => "<img src='".IMAGES."smiley/shock.gif' alt='smiley' />",
			"#\:p#si" => "<img src='".IMAGES."smiley/pfft.gif' alt='smiley' />",
			"#b\)#si" => "<img src='".IMAGES."smiley/cool.gif' alt='smiley' />",
			"#\:d#si" => "<img src='".IMAGES."smiley/grin.gif' alt='smiley' />",
			"#\:@#si" => "<img src='".IMAGES."smiley/angry.gif' alt='smiley' />"
		);
		foreach($smiley as $key=>$smiley_img) $message = preg_replace($key, $smiley_img, $message);
		return $message;
	}
	  
	function descript($text,$striptags=true) {
		$search = array("40","41","58","65","66","67","68","69","70",
			"71","72","73","74","75","76","77","78","79","80","81",
			"82","83","84","85","86","87","88","89","90","97","98",
			"99","100","101","102","103","104","105","106","107",
			"108","109","110","111","112","113","114","115","116",
			"117","118","119","120","121","122"
			);
		$replace = array("(",")",":","a","b","c","d","e","f","g","h",
			"i","j","k","l","m","n","o","p","q","r","s","t","u",
			"v","w","x","y","z","a","b","c","d","e","f","g","h",
			"i","j","k","l","m","n","o","p","q","r","s","t","u",
			"v","w","x","y","z"
			);
		$entities = count($search);
		for ($i=0;$i < $entities;$i++) $text = preg_replace("#(&\#)(0*".$search[$i]."+);*#si", $replace[$i], $text);
		$text = preg_replace('#(&\#x)([0-9A-F]+);*#si', "", $text);
		$text = preg_replace('#(<[^>]+[\\"\'\s])(onmouseover|onmousedown|onmouseup|onmouseout|onmousemove|onclick|ondblclick|onload|xmlns)[^>]*>#iU', ">", $text);
		$text = preg_replace('#([a-z]*)=([\`\'\"]*)script:#iU', '$1=$2nojscript...', $text);
		$text = preg_replace('#([a-z]*)=([\`\'\"]*)javascript:#iU', '$1=$2nojavascript...', $text);
		$text = preg_replace('#([a-z]*)=([\'\"]*)vbscript:#iU', '$1=$2novbscript...', $text);
		$text = preg_replace('#(<[^>]+)style=([\`\'\"]*).*expression\([^>]*>#iU', "$1>", $text);
		$text = preg_replace('#(<[^>]+)style=([\`\'\"]*).*behaviour\([^>]*>#iU', "$1>", $text);
		if ($striptags) {
			do {
		        	$thistext = $text;
				$text = preg_replace('#</*(applet|meta|xml|blink|link|style|script|embed|object|iframe|frame|frameset|ilayer|layer|bgsound|title|base)[^>]*>#i', "", $text);
			} while ($thistext != $text);
		}
		return $text;
	}


	

	function showdate($format, $val) {
				$settings = new DB("settings");
				$settings->select();
				$settings->nextRecord();
				return strftime($settings->$format, $val + ($settings->timeoffset*3600));  
	}
	
	function trimlink($text, $length) {
		$dec = array("&", "\"", "'", "\\", '\"', "\'", "<", ">");
		$enc = array("&amp;", "&quot;", "&#39;", "&#92;", "&quot;", "&#39;", "&lt;", "&gt;");
		$text = str_replace($enc, $dec, $text);
		if (strlen($text) > $length) $text = substr($text, 0, ($length-3))."...";
		$text = str_replace($dec, $enc, $text);
		return $text;
	}

?>